<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 求符合要求的结对方式（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>用一个数组A代表程序员的工作能力&#xff0c;公司想通过结对编程的方式提高员工的能力&#xff0c;假设结对后的能力为两个员工的能力之和&#xff0c;求一共有多少种结对方式使结对后能力为N。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<blockquote> 
 <p>5<br /> 1 2 2 2 3<br /> 4</p> 
</blockquote> 
<p>第一行为员工的总人数&#xff0c;取值范围[1,1000]</p> 
<p>第二行为数组A的元素&#xff0c;每个元素的取值范围[1,1000]</p> 
<p>第三行为N的值&#xff0c;取值范围[1,1000]</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<blockquote> 
 <p>4</p> 
</blockquote> 
<p>满足结对后能力为N的结对方式总数。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5<br /> 1 2 2 2 3<br /> 4</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">满足要求的结对方式为&#xff1a;A[0]和A[4]&#xff0c;A[1]和A[2]&#xff0c;A[1]和A[3]&#xff0c;A[2]和A[3]。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题应该只能用暴力方法求解所有两两组队。</p> 
<p>但是员工的总人数&#xff0c;取值范围[1,1000]&#xff0c;这意味着O(n^2)时间复杂度可能会超时&#xff0c;因此我们需要做一些优化。</p> 
<p></p> 
<p>我们可以先将数组A升序&#xff0c;然后外层循环 i 范围0 ~ A.len-1&#xff0c;内层循环 j 范围 A.len-1 ~ i&#43;1&#xff0c;如果遇到A[i]&#43;A[j] &#61;&#61;&#61; N&#xff0c;则计数&#43;&#43;&#xff0c;如果遇到A[i]&#43;A[j] &lt; N&#xff0c;则说明内层后续遍历出来的元素都无法满足要求&#xff0c;则内层循环终止&#xff0c;继续下一次外层。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 3) {
    const arr &#61; lines[1].split(&#34; &#34;).map(Number);
    const n &#61; parseInt(lines[2]);

    console.log(getResult(arr, n));

    lines.length &#61; 0;
  }
});

function getResult(arr, n) {
  arr.sort((a, b) &#61;&gt; a - b);

  let ans &#61; 0;
  for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
    for (let j &#61; arr.length - 1; j &gt;&#61; i &#43; 1; j--) {
      let sum &#61; arr[i] &#43; arr[j];
      if (sum &#61;&#61;&#61; n) ans&#43;&#43;;
      if (sum &lt; n) break;
    }
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  // 输入获取
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int total &#61; sc.nextInt();

    int[] arr &#61; new int[total];
    for (int i &#61; 0; i &lt; total; i&#43;&#43;) {
      arr[i] &#61; sc.nextInt();
    }

    int n &#61; sc.nextInt();

    System.out.println(getResult(arr, n));
  }

  // 算法入口
  public static int getResult(int[] arr, int n) {
    Arrays.sort(arr);

    int ans &#61; 0;
    for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      for (int j &#61; arr.length - 1; j &gt;&#61; i &#43; 1; j--) {
        int sum &#61; arr[i] &#43; arr[j];
        if (sum &#61;&#61; n) ans&#43;&#43;;
        else if (sum &lt; n) break;
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
total &#61; int(input())
arr &#61; list(map(int, input().split()))
n &#61; int(input())


# 算法入口
def getResult():
    arr.sort()

    ans &#61; 0
    for i in range(total):
        for j in range(total - 1, i, -1):
            sumV &#61; arr[i] &#43; arr[j]

            if sumV &#61;&#61; n:
                ans &#43;&#61; 1
            elif sumV &lt; n:
                break

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int cmp(const void *a, const void *b) {
    return (*(int *) a) - (*(int *) b);
}

int main() {
    int nums_size;
    scanf(&#34;%d&#34;, &amp;nums_size);

    int nums[nums_size];
    for (int i &#61; 0; i &lt; nums_size; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;nums[i]);
    }

    int n;
    scanf(&#34;%d&#34;, &amp;n);

    qsort(nums, nums_size, sizeof(int), cmp);

    int ans &#61; 0;
    for (int i &#61; 0; i &lt; nums_size; i&#43;&#43;) {
        for (int j &#61; nums_size - 1; j &gt;&#61; i &#43; 1; j--) {
            int sum &#61; nums[i] &#43; nums[j];

            if (sum &#61;&#61; n) ans&#43;&#43;;
            else if (sum &lt; n) break;
        }
    }

    printf(&#34;%d\n&#34;, ans);

    return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>